
package filter;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;

@WebFilter("/*")
public class LoginFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		/*
		 * chain.doFilter(request, response); // // //
		 */

		// /*
		String path = ((HttpServletRequest) request).getRequestURI(); //
		System.out.println("Filtro: " + path);

		HttpSession session = ((HttpServletRequest) request).getSession(false);

		if (path.toLowerCase().contains("login")
				|| path.toLowerCase().contains("javax.faces.resource")) {
			chain.doFilter(request, response);
		}
		else if (session != null && session.getAttribute("login") != null) {
			chain.doFilter(request, response);
		}
		else {
			String contextPath = ((HttpServletRequest) request)
					.getContextPath();
			((HttpServletResponse) response).sendRedirect(contextPath
					+ "/login.xhtml");
		} //

	}




	@Override
	public void destroy() {

	}




	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}
}
